﻿using System.Threading.Tasks;
using DailyDiet.Models;
using DailyDiet.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Forms;

namespace DailyDiet.ViewModels
{
    /// <summary>
    /// 菜品详情页ViewModel
    /// </summary>
    public class DietsDetailPageViewModel : ViewModelBase
    {

        /******** 构造函数 ********/

        /// <summary>
        /// 收藏存储
        /// </summary>
        private IFavoriteStorageService _favoriteStorage;

        /// <summary>
        /// 菜品详情页ViewModel
        /// </summary>
        /// <param name="favoriteStorage">收藏存储</param>
        public DietsDetailPageViewModel(IFavoriteStorageService favoriteStorage)
        {
            _favoriteStorage = favoriteStorage;
        }

        /******** 绑定属性 ********/

        /// <summary>
        /// 菜品
        /// </summary>
        private Diet _diet;

        public Diet Diet
        {
            get => _diet;
            set
            {
                Set(nameof(Diet), ref _diet, value);
                _isNewDiet = true;
            }
        }

        /// <summary>
        /// 收藏
        /// </summary>
        private Favorite _favorite;

        public Favorite Favorite
        {
            get => _favorite;
            set =>
                Set(nameof(Favorite), ref _favorite, value);
        }

        //私有变量
        private Shell _shell;
        public Shell Shell =>
            _shell ?? (_shell = Shell.Current);

        /******** 绑定命令 ********/

        /// <summary>
        /// 页面显示命令
        /// </summary>
        private RelayCommand _pageAppearingCommand;
        /// <summary>
        /// 页面显示命令
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ?? (_pageAppearingCommand =
                new RelayCommand(async () => await PageAppearingCommandFuction()));

        public async Task PageAppearingCommandFuction() {
            if (!_isNewDiet) {
                return;
            }

            _isNewDiet = false;

            var  favorite = await _favoriteStorage.GetFavoriteAsync(Diet.Id) ??
                new Favorite {DietId = Diet.Id};
            _isFavorite = favorite.IsFavorite;
            Favorite = favorite;
        }

           /// <summary>
        /// 页面回退命令
        /// </summary>
        private RelayCommand _backCommand;
        /// <summary>
        /// 页面回退命令
        /// </summary>
        public RelayCommand BackCommand =>
            _backCommand ?? (_backCommand =
                new RelayCommand(async () => await BackCommandFuction()));

        public async Task BackCommandFuction() {
            await Shell.CurrentPage.Navigation.PopAsync();
        }

        /// <summary>
        /// 收藏切换命令
        /// </summary>
        private RelayCommand _favoriteToggledCommand;
        /// <summary>
        /// 收藏切换命令
        /// </summary>
        public RelayCommand FavoriteToggledCommand =>
            _favoriteToggledCommand ?? (_favoriteToggledCommand = new RelayCommand(async () => await FavoriteToggledCommandFuction()));
        public async Task FavoriteToggledCommandFuction()
        {
            if (_isFavorite == Favorite.IsFavorite)
            {
                return;
            }

            _isFavorite = Favorite.IsFavorite;

            await _favoriteStorage.RefreshFavorite(Favorite);
        }

        /// <summary>
        /// 是否为新的食品
        /// </summary>
        private bool _isNewDiet;
        /// <summary>
        /// 诗词从数据库读出的原始状态
        /// </summary>
        public bool _isFavorite;

    }
}


